<template>
  <basic-container v-if="showType === 'list'">
    <avue-crud
      ref="crud"
      :table-loading="tableLoading"
      :data="tableData"
      :option="tableOption"
      :page.sync="page"
      @on-load="onLoad"
      @current-change="currentChange"
      @size-change="sizeChange"
      @row-del="deleteRow"
    >
      <template slot-scope="scope" slot="menuLeft">
        <el-button type="primary" icon="el-icon-plus" size="small" @click.stop="showType = 'add'">新增</el-button>
      </template>
    </avue-crud>

  </basic-container>
  <create-measure v-else-if="showType === 'add'"></create-measure>
</template>
<script>

import { griffinService } from '@/api/griffin'

import createMeasure from './measure-create/measure-create';

export default {
  components: {
    'create-measure': createMeasure
  },
  data(){
    return {
      showType: 'list',
      tableLoading: false,
      tableData: [],
      tableOption: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: false,
        editBtn: false,
        addBtn: false,
        viewBtn: true,
        // delBtn: false,
        menuWidth: 150,
        dialogClickModal: false,
        column: [
          {
            label: '名称',
            prop: 'name',
            span: 24
          },
          {
            label: '类型',
            prop: 'dqType',
          },
          {
            label: '描述',
            prop: 'description'
          }
        ]
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
    }
  },
  mounted(){


  },
  methods: {

    onLoad(){
      this.tableLoading = true;
      griffinService.getMeasuresList({
        projectId: this.$store.state.dag.projectId
      }).then(
        res => {
          this.tableLoading = false;
          let tableData = [];
          (res.data.data || []).forEach(
            item => {
              tableData.push({
                ...item,
                dqType: item['dq.type']
              })
            }
          )
          this.page.total = res.data.totalCounts;
          this.tableData = tableData.reverse();
        }
      )
    },
    deleteRow(row){
      this.$confirm(`确定要删除${row.name}？` , `删除指标` , {
        beforeClose: (action , instance , done) => {
          if(action === 'confirm'){
            instance.confirmButtonLoading = true;
            griffinService.deleteMeasures(row.id).then(
              () => {
                instance.confirmButtonLoading = false;
                this.$message({
                  type: 'success',
                  message: '操作成功'
                });
                this.onLoad();
                done();
              }
            ).catch(() => {
              instance.confirmButtonLoading = false;
              done();
            })
          }else{
            done();
          }
        }
      });
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    }

  }

}
</script>